﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Imaginarium.team;
using Microsoft.Xna.Framework.Graphics;
using Imaginarium.battleSistem;
using GameEngine.Scenes;
using GuiLibrary.layouts;
using GuiLibrary.Components;
using Microsoft.Xna.Framework;
using GameEngine.utils.console;
using GuiLibrary;

namespace Imaginarium.gui
{
    public class BattleCommandGui : Gui , IOnCancel
    {
        public Character performerCharacter { get; set; }

        public BattleCommandGui(Character performer)
            :base()
        {
            performerCharacter = performer;
        }

        public override void LoadContent()
        {
            #region ObtainRequiredData
            List<Element> optionElements = new List<Element>();
            if (performerCharacter is Player)
            {
                Player player = (Player)performerCharacter;
                foreach (BattleOption bo in player.currentJob.job.battleOptions)
                    //optionElements.Add(new Element(bo.name, new BattleOption(bo.type,bo.name)));
                    optionElements.Add(new ImaginariumElement(bo));
            }
            else if (performerCharacter is Enemy)
            {
                Enemy enemy = (Enemy)performerCharacter;
                foreach (BattleOption bo in enemy.battleOptions)
                    optionElements.Add(new ImaginariumElement(bo));
            }
            #endregion

            #region CreateGuiComponents
            SpriteFont font = Imaginarium.ImaginariumGame.getInstance().Content.Load<SpriteFont>(CodeParameters.battleCommandFont);
            List<Component> textComponents = new List<Component>();
            basePanel = new Panel(position, GUIParameters.backgroundBattleCommandGUI());
            basePanel.components = textComponents;
            foreach (Element option in optionElements)
            {
                textComponents.Add(new SelectableLabel(basePanel, new Vector2(), new Text(font, option.name, Color.White, GameEngine.animations.OriginType.LEFT_UP),option));
            }
            basePanel.layout = new ListLayout(basePanel, GUIParameters.marginsBattleCommandGUI, true, this, this, null);
            sceneResolution = basePanel.background.getCurrentAnimation().getFrameSize();
            basePanel.layout.refresh();
            #endregion
        }

        public override void onSelectedElement(Element e) {
            if(e is ImaginariumElement){
                ImaginariumElement ie = (ImaginariumElement)e;
                if (ie.type == ImaginariumElement.objectType.BATTLE_COMMAND) //always should happen
                {
                    BattleOption bo = (BattleOption)e.associatedObject;
                    SceneManager sm = SceneManager.getInstance();
                    switch (bo.type)
                    {
                        case BattleOption.BattleOptionEnum.Ability:
                            sm.scenes.Add(createSkillSelectorGui());
                            break;
                        case BattleOption.BattleOptionEnum.Inventory:
                            sm.scenes.Add(createItemSelectorGui());
                            break;
                        case BattleOption.BattleOptionEnum.Attack:
                            attackOptionSelected();
                            break;
                        case BattleOption.BattleOptionEnum.Defend:
                            setCharacterOnDefenseMode();
                            break;
                    }
                }
                else
                {
                    throw new Exception("Pero que carajo");
                }
            }
        }

        private void setCharacterOnDefenseMode()
        {
            BattleManager.getInstance().turnExpert.setDefendCommand();
        }

        private void attackOptionSelected() {
            BattleManager.getInstance().turnExpert.setAttackCommand();
            SceneManager.getInstance().scenes.Add(createTargetSelectorGui());
        }
        private Scene createTargetSelectorGui()
        {
            SelectTargetGui stgui = new SelectTargetGui(BattleManager.getInstance().turnExpert.m_Teams);
            stgui.isPopUp = true;
            stgui.position = new Vector2(0, 550);
            stgui.LoadContent();
            return stgui;
        }

        private Scene createItemSelectorGui()
        {
            throw new NotImplementedException();
        }

        private Scene createSkillSelectorGui()
        {
            SelectSkillGui skgui = new SelectSkillGui(performerCharacter);
            skgui.isPopUp = true;
            skgui.position = new Vector2(0, 550);
            skgui.LoadContent();
            return skgui;
        }

        #region IOnCancel Members

        public void onCancel()
        {
            if (BattleManager.getInstance().turnExpert.currentCharacter > 0)
            {
                BattleManager.getInstance().turnExpert.cancelLastCommand();
                ImaginariumConsole.getInstance().write("Removido un command del stack");
            }
        }

        #endregion
    }
}